package org.jeecg.modules.rooms.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesRoomBuildingFloorDto;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.Vo.FloorWithBuildingVo;
import org.jeecg.modules.rooms.entity.CesRoomBuildingFloor;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.mapper.CesRoomBuildingFloorMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.rooms.mapper.CesRoomsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼栋楼层表 服务实现类
 * </p>
 *
 * @author fendo
 * @since 2023-03-06
 */
@Service
public class CesRoomBuildingFloorServiceImpl extends ServiceImpl<CesRoomBuildingFloorMapper, CesRoomBuildingFloor> implements IService<CesRoomBuildingFloor> {

    @Resource
    private CesRoomBuildingFloorMapper buildingFloorMapper;

    @Resource
//    private CesRoomsServiceImpl cesroomsService;
    CesRoomsMapper cesRoomsMapper;

    /**
     * 获取楼栋楼层树
     * @param hotelId
     * @return
     */
    public Result tree(String hotelId){
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //查询数据
        List<CesRoomBuildingFloor> list = buildingFloorMapper.selectList(Wrappers.<CesRoomBuildingFloor>lambdaQuery().eq(CesRoomBuildingFloor::getHotelId,hotelId).eq(CesRoomBuildingFloor::getInvalid,false));
        list.forEach(v -> {
            nodeList.add(new TreeNode<>(v.getId(),v.getParentId(),v.getName(),0));
        });
        if(CollUtil.isEmpty(nodeList)) return Result.OK(new ArrayList<>());
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        return Result.OK(treeList);
    }


    /**
     * 创建楼栋或者楼层
     * @param floorDto
     * @return
     */
    public Result create(CesRoomBuildingFloorDto floorDto){
        QueryWrapper<CesRoomBuildingFloor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CesRoomBuildingFloor.HOTELID,floorDto.getHotelId());
        queryWrapper.eq(CesRoomBuildingFloor.TYPE, floorDto.getType().intValue());
        queryWrapper.eq(CesRoomBuildingFloor.NAME,floorDto.getName());
        if(CouponEnums.RoomBuildingFloorTypeEnum.FLOOR.code() == floorDto.getType().intValue()){
            queryWrapper.eq(CesRoomBuildingFloor.PARENT_ID, floorDto.getParentId());
        }
       List<CesRoomBuildingFloor> floorList =  buildingFloorMapper.selectList(queryWrapper);
        if(!CollectionUtil.isEmpty(floorList)) return Result.error("该名称已有!");
        CesRoomBuildingFloor buildingFloor = new CesRoomBuildingFloor();
        buildingFloor.setName(floorDto.getName());
        buildingFloor.setHotelId(floorDto.getHotelId());
        buildingFloor.setParentId(floorDto.getParentId());
        buildingFloor.setType(floorDto.getType());
        buildingFloor.setCreateAt(LocalDateTime.now());
        buildingFloor.setUpdateAt(LocalDateTime.now());
        buildingFloor.setInvalid(false);
        buildingFloorMapper.insert(buildingFloor);
        return Result.OK("创建成功!");
    }


    /**
     * 修改楼层或者楼栋
     * @param floorDto
     * @return
     */
    public Result modify(CesRoomBuildingFloorDto floorDto){
        QueryWrapper<CesRoomBuildingFloor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CesRoomBuildingFloor.HOTELID,floorDto.getHotelId());
        queryWrapper.eq(CesRoomBuildingFloor.TYPE, floorDto.getType().intValue());
        queryWrapper.eq(CesRoomBuildingFloor.NAME,floorDto.getName());
        if(CouponEnums.RoomBuildingFloorTypeEnum.FLOOR.code() == floorDto.getType().intValue()){
            queryWrapper.eq(CesRoomBuildingFloor.PARENT_ID, floorDto.getParentId());
        }
        queryWrapper.ne(CesRoomBuildingFloor.ID,floorDto.getId());

        List<CesRoomBuildingFloor> floorList =  buildingFloorMapper.selectList(queryWrapper);
        if(!CollectionUtil.isEmpty(floorList)) return Result.error("该名称已有!");
        CesRoomBuildingFloor  cesRoomBuildingFloor =  buildingFloorMapper.selectById(floorDto.getId());
        if(ObjectUtils.isEmpty(cesRoomBuildingFloor)) return Result.error("该楼层或者楼栋未找到!");

        cesRoomBuildingFloor.setName(floorDto.getName());
        cesRoomBuildingFloor.setUpdateAt(LocalDateTime.now());
        buildingFloorMapper.updateById(cesRoomBuildingFloor);
        return Result.OK("修改成功!");
    }


    /**
     * 删除楼栋或者楼层
     * @param floorDto
     * @return
     */
    public Result delete(CesRoomBuildingFloorDto floorDto){
        CesRoomBuildingFloor  cesRoomBuildingFloor =  buildingFloorMapper.selectById(floorDto.getId());
        if(ObjectUtils.isEmpty(cesRoomBuildingFloor)) return Result.error("该楼层或者楼栋未找到!");

        List<CesRoomBuildingFloor> children = buildingFloorMapper.selectList(Wrappers.<CesRoomBuildingFloor>lambdaQuery()
                .eq(CesRoomBuildingFloor::getParentId,cesRoomBuildingFloor.getId())
                .eq(CesRoomBuildingFloor::getInvalid,false)
        );

        if(!ObjectUtils.isEmpty(children)) return Result.error("该楼栋下还有楼栋数据,请全部删除后再删除楼栋!");
        long count = cesRoomsMapper.selectCount(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getFloorId, floorDto.getId())
                    .eq(CesRooms::getInvalid, false));
        if (count > 0) {
            return Result.error("该楼栋下还有房间,请先删除房间!");
        }
        cesRoomBuildingFloor.setInvalid(true);
        cesRoomBuildingFloor.setUpdateAt(LocalDateTime.now());
        buildingFloorMapper.updateById(cesRoomBuildingFloor);

        return Result.ok("删除成功!");
    }


    /**
     * 根据IDs 查询 楼栋楼层
     * @param ids
     * @return
     */
    public List<CesRoomBuildingFloor> getBuildFloorsByIds(List<String> ids){
        if(CollectionUtil.isEmpty(ids)) return new ArrayList<>();

        List<CesRoomBuildingFloor> buildingFloors = buildingFloorMapper.selectList(Wrappers.<CesRoomBuildingFloor>lambdaQuery()
                .in(CesRoomBuildingFloor::getId, ids)
                .eq(CesRoomBuildingFloor::getInvalid,false)
        );
        return buildingFloors;
    }


    /**
     * 查询单个数据
     * @param id
     * @return
     */
    public CesRoomBuildingFloor getBuildFloorById(String id){
        if(StringUtils.isBlank(id)) return null;
        CesRoomBuildingFloor buildingFloor = buildingFloorMapper.selectById(id);
        return buildingFloor;
    }

    /**
     * 根据上级Id获取下级数据
     * @param parentId
     * @return
     */
    public Result queryByParentId(String parentId){
        //查询数据
        List<CesRoomBuildingFloor> list = buildingFloorMapper.selectList(Wrappers.<CesRoomBuildingFloor>lambdaQuery().eq(CesRoomBuildingFloor::getParentId,parentId).eq(CesRoomBuildingFloor::getInvalid,false));
        return Result.OK(list);
    }

    /**
     * 根据酒店获取楼层数据
     * @param hotelId
     * @return
     */
    public  List<FloorWithBuildingVo> getHotelFloors(String hotelId) {
        return baseMapper.buildingFloors(hotelId);
    }

}
